<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use \Cache;
class MenuController extends Controller
{	public $token=null;
    public function __construct(){
           if (!Cache::has('token3'))
                        {

                                $appid ="wx61a5ce5230a32e92";
                                $appsecret ="14ba21479bce9ff60ba93185cbc5e4bb";
                                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

                                $ch = curl_init();
                                curl_setopt($ch, CURLOPT_URL, $url);
                                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                $output = curl_exec($ch);
                                curl_close($ch);
                                $jsoninfo = json_decode($output, true);
                                $access_token = $jsoninfo["access_token"];
                                 Cache::put('token3',$access_token, 2);
                                $this->token = Cache::get('token3');

                                //memcache_set($mmc,"token",$access_token,0,7200);
                                //$token=memcache_get($mmc,"token");
                        }
			$this->token = Cache::get('token3');

    }
	public function insert(){
//echo $this->token;exit;
	$jsonmenu = '{
      "button":[
      {
            "name":"一键查询",
           "sub_button":[
		  {
                "type":"view",
                "name":"阿里版",
                "url":"http://www.laoyou1.me/alcenter"
        	 },
	        {
                "type":"view",
                "name":"标准版",
                "url":"http://www.laoyou1.me/center"
            },
	{
		"type":"view",
		"name":"精简版",
		"url":"http://www.laoyou1.me/nncenter"
	 }


	]
       },
       {
            "name":"设备管理",
           "sub_button":[
            {
                "type":"view",
                "name":"设备绑定",
                "url":"http://www.laoyou1.me/centers"
            },
            {
                "type":"view",
                "name":"关闭灶台",
                "url":"http://www.laoyou1.me/zaotai"
            }


]
       },
       {
           "name":"联系我们",
           "sub_button":[
         	{
               "type":"view",
               "name":"公司简介",
               "url":"http://www.laoyou1.me/production"

            },

            {
               "type":"view",
               "name":"意见反馈",
               "url":"http://www.laoyou1.me/ocenter"

            },

	    {
               "type":"view",
               "name":"待开发",
               "url":"http://www.laoyou1.me/npost"

            }


            ]
       }
	   ]
 }';

//$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token;
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->token;
$result =$this-> https2_request($url, $jsonmenu);
var_dump($result);
	}
	public function delete(){
		$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->token;
		$result =$this-> https2_request($url);
		var_dump($result);
	}
	public function wechat_access_token(){	
		return $this->token;
	}
	public function https2_request($url,$data = null){
                 $curl = curl_init();
                 curl_setopt($curl, CURLOPT_URL, $url);
                 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
                 if (!empty($data)){
                  curl_setopt($curl, CURLOPT_POST, 1);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                 }
                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                 $output = curl_exec($curl);
                 curl_close($curl);
                 return $output;

        }
	public function delete_token(){
		$value = Cache::pull('toke');
		echo $value?11:00;
	}
	
}
